package com.agile.familytree.business;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.agile.familytree.datastructure.Family;
import com.agile.familytree.datastructure.Person;
import com.agile.familytree.util.FamilyUtil;

public class Validator {
	
	DateFormat formatter = new SimpleDateFormat("dd MMM yyyy"); 
    
	public void validateDeathBeforeBirth(List<Person> lPerson) throws Exception {
		
	    Date birthDate = new Date();
	    Date deathDate = new Date(); 
	    
	    System.out.println("------------------------------------------------");
	    System.out.println("\nTask4\n");
		//person
		for (Person person : lPerson){
			
			if(!FamilyUtil.getInstance().getHashIdAlive().get(person.getIdentifier()) ){
				
				if(person.getBirt().trim().length()>8 && person.getDeat().trim().length()>8) {
					
					birthDate = (Date)formatter.parse(person.getBirt());
					deathDate = (Date)formatter.parse(person.getDeat());
					
					if( birthDate.after(deathDate) ){
						System.out.println("****ERROR - birth date is after the death date");
						System.out.println(person.getName() +", " +person.getSex() + ", Birth Date:"+ person.getBirt()+", Death Date:" +person.getDeat());
					}
					else if(birthDate.before(deathDate)){
						System.out.println("No Error - birth date is before the death date");
						System.out.println(person.getName() +", " +person.getSex() + ", Birth Date:"+ person.getBirt()+", Death Date:" +person.getDeat());
					}
				}
			}
		}

	}
	
	public void validateMarriageToTheDead(List<Family> lFamily) throws Exception {
		
	    Date marriedDate = new Date();
	    Date deathDate = new Date(); 

		//family
		System.out.println("\nTask 5 \n");
		for (Family family : lFamily){
								
				//System.out.println("Family:"+family.getIdentifier() + ", Husbund:"+FamilyUtil.getInstance().getHashIdName().get(family.getHusb().trim()) +", Wife:" +FamilyUtil.getInstance().getHashIdName().get(family.getWife().trim()) +", Married Date:"+family.getMarr()+", Divorced Date:"+family.getDiv());
								
				//check husband
				if( (!FamilyUtil.getInstance().getHashIdAlive().get(family.getHusb().trim()) ) ){ 
					if(family.getMarr().trim().length()>8){
						marriedDate = (Date)formatter.parse(family.getMarr());
						deathDate = (Date)formatter.parse(FamilyUtil.getInstance().getHashIdDeathDate().get(family.getHusb().trim()));
					
						if( marriedDate.after(deathDate) ){
							System.out.println("****ERROR - married date is after the husband's death date");
							System.out.println("Family:"+family.getIdentifier() + ", Husbund:"+FamilyUtil.getInstance().getHashIdName().get(family.getHusb().trim()) +
									", Wife:" +FamilyUtil.getInstance().getHashIdName().get(family.getWife().trim()) +", Married Date:"+family.getMarr()+
									", Divorced Date:"+family.getDiv());
						}
						else if(marriedDate.before(deathDate)){
							System.out.println("No Error - married date is before the death date");
						}
					}
				}
				
				//check wife
				if(!FamilyUtil.getInstance().getHashIdAlive().get(family.getWife().trim()) ){
					if(family.getMarr().trim().length()>8){
						marriedDate = (Date)formatter.parse(family.getMarr());
						deathDate = (Date)formatter.parse(FamilyUtil.getInstance().getHashIdDeathDate().get(family.getWife().trim()));
					
						if( marriedDate.after(deathDate) ){
							System.out.println("****ERROR - married date is after the wife's death date");
							System.out.println("Family:"+family.getIdentifier() + ", Husbund:"+FamilyUtil.getInstance().getHashIdName().get(family.getHusb().trim()) +
									", Wife:" +FamilyUtil.getInstance().getHashIdName().get(family.getWife().trim()) +", Married Date:"+family.getMarr()+
									", Divorced Date:"+family.getDiv());
						}
						else if(marriedDate.before(deathDate)){
							System.out.println("No Error - married date is before the death date");
						}
					
					}
				
				}
		}
	
	}
	
	public void validateMarriedToSibling(List<Person> lPerson, List<Family> lFamily) throws Exception {
		
		//family
		System.out.println("\nTask 6 - check married to sibling \n");
		
		String [] children;

		//family -check married to sibling
		String idHusband = "";
		String idWife = "";
		boolean foundHusband = false;
		boolean foundWife = false;
		boolean marriedToSibling = false;
		
		for (Family family : lFamily){
			
			System.out.println("Family:"+family.getIdentifier() + ", Husband:"+FamilyUtil.getInstance().getHashIdName().get(family.getHusb().trim()) +
					           ", Wife:" +FamilyUtil.getInstance().getHashIdName().get(family.getWife().trim()) +
 				               ", Married Date:"+family.getMarr()+", Divorced Date:"+family.getDiv()+", Current:"+family.isCurrent());

			idHusband = family.getHusb().trim();
			idWife = family.getWife().trim();
			
			for (Family familyInner : lFamily){
				
				children = familyInner.getAllChildren();
				
				for(String child : children){
					
					if(child.trim().equals(idHusband)){
						foundHusband = true;
					}
					else if(child.trim().equals(idWife)){
						foundWife = true;
					}
				}
				
				if (foundHusband && foundWife){
					marriedToSibling = true;
					System.out.println("****ERROR : "+FamilyUtil.getInstance().getHashIdName().get(idHusband) +" and "+FamilyUtil.getInstance().getHashIdName().get(idWife)+" are sibling and married");
					break;
				}
				
				foundHusband = false;
				foundWife = false;
				
			}
		}
	}

	
	public void validatePoligamy(List<Family> lFamily) throws Exception {
		
		//family
		System.out.println("\nTask 7 - check poligamy\n");
		
		String idHusband = "";
		String idWife = "";
		boolean isCurrent =false;
		
		outer:for (Family family : lFamily){
			
			System.out.println("-------------------------------------");
			
			System.out.println("Family:"+family.getIdentifier() + ", Husband:"+FamilyUtil.getInstance().getHashIdName().get(family.getHusb().trim()) +
			           ", Wife:" +FamilyUtil.getInstance().getHashIdName().get(family.getWife().trim()) +
			           ", Married Date:"+family.getMarr()+", Divorced Date:"+family.getDiv()+", Current:"+family.isCurrent());
			
			idHusband = family.getHusb().trim();
			idWife = family.getWife().trim();
			isCurrent = family.isCurrent();
			
			for (Family familyInner : lFamily){
				
				if(!familyInner.getWife().trim().equals(idWife) &&  familyInner.getHusb().trim().equals(idHusband) && familyInner.isCurrent() == isCurrent){
					
					System.out.println("****ERROR Poligamy: "+FamilyUtil.getInstance().getHashIdName().get(idHusband) +" married to more than one spouse");
					break outer;

				}
				
			}
		}

		
	}

}
